package com.tuling.controller;

import com.tuling.domain.Cart;
import com.tuling.domain.Fruit;
import com.tuling.domain.User;
import com.tuling.service.CartService;
import com.tuling.service.CartServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "cartServlet", value = "/cartServlet")
public class CartServlet extends HttpServlet {
    private CartService cartService = new CartServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取参数
        String method = request.getParameter("method");
        // 根据method做不同处理
        if (method.equals("add")) {
            // 添加购物车
            add(request, response);
        } else if (method.equals("show")) {
            show(request, response);
        } else if (method.equals("num")) {
            num(request, response);
        } else if (method.equals("del")) {
            del(request, response);
        }
    }

    private void del(HttpServletRequest request, HttpServletResponse response) {
        String uidStr = request.getParameter("uid");
        String fidStr = request.getParameter("fid");
        String str = request.getParameter("str");
        int uid = Integer.parseInt(uidStr);
        int fid = Integer.parseInt(fidStr);
        if (uid!=0) {
            Cart cart = cartService.find(uid, fid);
            if ("cart".equals(str)) {
                cart.setIsCart(false);
            } else {
                cart.setIsStar(false);
            }
            if (cart.getIsCart() || cart.getIsStar()) {
                cartService.update(uid, cart);
            } else {
                cartService.del(uid, fid);
            }
        }
        try {
            request.getRequestDispatcher("/cartServlet?method=show&uid="+ uid + "&boob=" + str).forward(request, response);
            List<Fruit> fruits = (List<Fruit>)request.getAttribute("fruits");
            System.out.println(fruits);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void num(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        int num = 0;
        if (user!=null) {
            List<Cart> carts = cartService.findByUid(user.getId());
            num = carts.size();
        }
        request.setAttribute("num", num);
    }

    private void show(HttpServletRequest request, HttpServletResponse response) {
        // 参数
        String uidStr = request.getParameter("uid");
        String boob = request.getParameter("boob");
        int uid = Integer.parseInt(uidStr);
        if (uid!=0) {
            if ("cart".equals(boob)) {
                List<Fruit> show = cartService.show(uid, true);
                request.setAttribute("fruits", show);
                try {
                    request.getRequestDispatcher("/showcart.jsp").forward(request, response);
                } catch (ServletException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                List<Fruit> show = cartService.show(uid, false);
                request.setAttribute("fruits", show);
                try {
                    request.getRequestDispatcher("/showstar.jsp").forward(request,response);
                } catch (ServletException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else {
            request.setAttribute("showError", true);
            try {
                request.getRequestDispatcher("/login.jsp").forward(request, response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void add(HttpServletRequest request, HttpServletResponse response) {
        String uidStr = request.getParameter("uid");
        String fidStr = request.getParameter("fid");
        String str = request.getParameter("str");
        int uid = Integer.parseInt(uidStr);
        int fid = Integer.parseInt(fidStr);

        if (uid!=0) {
            Cart cart = cartService.find(uid, fid);
            if (cart != null) {
                if (str.equals("cart")) {
                    if (!cart.getIsCart()) {
                        cart.setIsCart(true);
                        cartService.update(uid, cart);
                    }
                } else {
                    if (!cart.getIsStar()) {
                        cart.setIsStar(true);
                        cartService.update(uid,cart);
                    }
                }
            } else {
                if (str.equals("cart")) {
                    cartService.add(uid, new Cart(uid, fid, false, true));
                } else {
                    cartService.add(uid, new Cart(uid, fid, true, false));
                }
            }
            try {
                request.getRequestDispatcher("/fruitServlet?method=info&uid="+uid+"&fid"+fid).forward(request, response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            request.setAttribute("showError", true);
            try {
                request.getRequestDispatcher("/login.jsp").forward(request, response);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
